package main.guis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import main.bots.Bot;
import main.bots.Methods;
import main.bots.ScriptManager;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ScriptStart.java
 *
 * Created on Nov 20, 2009, 11:42:11 PM
 */

/**
 *
 * @author Austin
 */
@SuppressWarnings("serial")
public class ScriptStart extends javax.swing.JFrame implements GUI {
	 private javax.swing.DefaultListModel listModel;
	 public ArrayList<Bot> scriptMap = new ArrayList<Bot>();
    /** Creates new form ScriptStart */
    public ScriptStart() {
    	 listModel = new javax.swing.DefaultListModel();
    	 setUpScripts();
        initComponents();
       
    }
    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {
        jTextArea1.setText(scriptMap.get(jList1.getSelectedIndex()).getDescription());
    }
    private void setUpScripts() {
    	Set<String> set= Methods.getClientToUse().scriptMan.BotMap.keySet();
    	Iterator<String> iter = set.iterator();
    	while(iter.hasNext()){
    	Bot currentKey = Methods.getClientToUse().scriptMan.BotMap.get(iter.next());
    	if(currentKey == null) {
    		System.out.println("Script was null on adding");
    		return;
    	}
    	System.out.println("Script added");
    	addListModelItem(currentKey.getScriptName());
    	scriptMap.add(currentKey);
    	}
		
	}
	public void addListModelItem(String scriptName) {
        System.out.println("adding " + scriptName);
	listModel.addElement(scriptName);
             //addItem(Integer.parseInt(item), itemName);
}

public void removeListModelItem(String item) {
	this.listModel.removeElement(item);
}

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jList1.setModel(listModel);
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);

        
        jScrollPane1.setViewportView(jList1);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        jButton1.setText("Start Script");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(266, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    @SuppressWarnings("deprecation")
	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
       ScriptManager.startBot(scriptMap.get(jList1.getSelectedIndex()));
       hide();
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ScriptStart().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration

	@Override
	public void showGUI() {
		
		new ScriptStart().setVisible(true);
		
	}

}
